#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 读取目录大小，并发送邮件 
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import os
m3_value = os.popen("du -s /m3/ |awk '{print $1}'").read()

#告警限额90G
alert_value_gb=90
alert_value_gb_about = alert_value_gb * 1000000

#假如目录统计大于90G，触发告警
if int(m3_value) > alert_value_gb_about:
    os.system("du -sh /m3/* > /tmp/m3.txt")
    os.system("kubectl get pv >> /tmp/m3.txt")
    os.system("du -sh /m3/nas-e836fd70-2d5f-43cb-88dd-S2AAAAXCD/underway-flink/* >> /tmp/m3.txt")

    mail_host="smtp.exmail.qq.com"
    mail_user="USER_NAME"
    mail_pass="USER_PASSWORD"
    sender = 'USER_NAMEMAIL'
    receivers = ['wangwuli@sumztech.com']  #

    message = MIMEMultipart()
    message['From'] = Header("[SUMZ自定义告警][阿里云]", 'utf-8')
    message['To'] =  Header("sumzer", 'utf-8')
    subject = '系统自动发送'
    message['Subject'] = Header(subject, 'utf-8')
    
    message.attach(MIMEText('[来着XXX.XX.XXX.XX crontab]:nas急速盘，m3存储目录使用量到达90G左右，可疑点，见报告附件', 'plain', 'utf-8'))
    
    att1 = MIMEText(open('/tmp/m3.txt', 'rb').read(), 'base64', 'utf-8')
    att1["Content-Type"] = 'application/octet-stream'

    att1["Content-Disposition"] = 'attachment; filename="m3.txt"'
    message.attach(att1)

    smtpObj = smtplib.SMTP_SSL(mail_host, 465)
    smtpObj.login(mail_user, mail_pass)  
    smtpObj.sendmail(sender, receivers, message.as_string())
    smtpObj.quit()

#0 4 * * * python /sumz/m3_capacity_help.py